﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Twittit.Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void registersubmit_Click(object sender, EventArgs e)
        {
            String name = NameTextbox.Text;
            String twittit = LoginTextbox.Text;
            String mail = EmailTextbox.Text;
            String p1 = PasswordTextbox.Text;
            String p2 = PasswordTextboxConfirm.Text;
            // TODO check twittit is unique
            if (valEmailAddress.IsValid && this.ChekTwittit(null, twittit) && p1 == p2)
            {
                Twittit.Business.UserService.getInstance().createUser(name, twittit, mail, p1);
                Session["username"] = twittit;
                Response.Redirect("Default.aspx");
            }
            else {
                if (!this.ChekTwittit(null, twittit))
                    RegisterErrorLabel.Text = "Echec : le pseudo souhaité est déjà pris";
                else if (p1 != p2)
                    RegisterErrorLabel.Text = "Echec : les deux mots de passe doivent être identiques";
                Session["username"] = null;
            }
        }

        private bool ChekTwittit(Object source, String value)
        {
            bool r = false;

            r = Twittit.Business.UserService.getInstance().checkTwittit(value);

            return r;
        }
    }
}